package com.ficus.road.maintain.dao;

import com.ficus.road.maintain.core.dao.BaseMongoDao;
import com.ficus.road.maintain.core.model.Hit;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.index.GeoSpatialIndexType;
import org.springframework.data.mongodb.core.index.GeospatialIndex;
import org.springframework.data.mongodb.core.index.Index;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class HitDao extends BaseMongoDao<Hit, String> {

    @Override
    public void indexOperation() {
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("hit_time", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("last_modification_time", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("attrs.disease_uid", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("first_hit_time", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("list.attrs.disease_uid", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("type", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("attrs.loop", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("attrs.status", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("attrs.handle_company_code", Sort.Direction.DESC).background());
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(new Index().on("attrs.is_top", Sort.Direction.DESC).background());
        GeospatialIndex geoIndex = new GeospatialIndex("attrs.location").typed(GeoSpatialIndexType.GEO_2DSPHERE);
        super.getMongoTemplate().indexOps(getCollectionName()).ensureIndex(geoIndex);
    }

    @Override
    public String getCollectionName() {
        return "hit";
    }

    @Override
    public Hit insert(Hit entity) {
        entity.setCreateTime(new Date(entity.getCreationTime() * 1000));
        return super.insert(entity);
    }

    @Override
    public Hit saveOrUpdate(Hit entity) {
        entity.setCreateTime(new Date(entity.getCreationTime() * 1000));
        return super.saveOrUpdate(entity);
    }
}
